//
//  SettingCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/7.
//

import UIKit

class SettingCell: UITableViewCell {
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        return label
    }()
    
    lazy var arrowImage: UIImageView = {
        return UIImageView(image: UIImage(named: "setting_right_arrow"))
    }()
    
    lazy var rightTitle: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textAlignment = .right
        label.textColor = .init(named: "gray_999999")
        return label
    }()
    
    lazy var autoSwitch: UISwitch = {
        let swi = UISwitch()
        swi.tintColor = .init(named: "gray_666666")
        swi.onTintColor = .init(named: "red_FE95B4")
        swi.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
        swi.rx.isOn.changed.subscribe(onNext: { [weak self] (isOn) in
            WNThread.wn_delay(self!.settingType == 0 ? 0.15 : 0) {
                self?.openClosure?(isOn)
            }
        }).disposed(by: rx.disposeBag)
        swi.isHidden = true
        return swi
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    var openClosure: BoolClosure?
        
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(titleLabel)
        contentView.addSubview(arrowImage)
        contentView.addSubview(rightTitle)
        contentView.addSubview(lineView)
        contentView.addSubview(autoSwitch)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
        }
        arrowImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.width.height.equalTo(24)
        }
        rightTitle.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(arrowImage.snp.left).offset(-5)
        }
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.bottom.right.equalTo(0)
            make.height.equalTo(0.5)
        }
        autoSwitch.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
        }
    }
    
    var settingType: Int? {
        didSet {
            lineView.isHidden = false
            arrowImage.isHidden = false
            autoSwitch.isHidden = true
            rightTitle.text = ""
            rightTitle.snp.updateConstraints { (make) in
                make.right.equalTo(arrowImage.snp.left).offset(-5)
            }
            switch settingType {
            case 0:
                arrowImage.isHidden = true
                autoSwitch.isHidden = false
                titleLabel.text = "自動解鎖下一章"
                autoSwitch.isOn = getAutoOpen()
//            case 1:
//                arrowImage.isHidden = true
//                autoSwitch.isHidden = false
//                titleLabel.text = "啟用通知"
//                autoSwitch.isOn = getEnableNot()
            case 1:
                titleLabel.text = "帳戶與安全"
            case 2:
                titleLabel.text = "用戶回饋"
                lineView.isHidden = true
            case 10:
                titleLabel.text = "聯絡我們"
                rightTitle.text = "wqg@clickfun.cn"
                rightTitle.snp.updateConstraints { (make) in
                    make.right.equalTo(arrowImage.snp.left).offset(-6)
                }
            case 11:
                titleLabel.text = "關於我們"
//            case 12:
//                titleLabel.text = "評價我們"
            case 12:
                arrowImage.isHidden = true
                titleLabel.text = "清除緩存"
                if wn_fileSizeOfCache() == 0 {
                    rightTitle.text = ""
                }else {
                    rightTitle.text = "\(wn_fileSizeOfCache())M"
                }
                rightTitle.snp.updateConstraints { (make) in
                    make.right.equalTo(arrowImage.snp.left).offset(18)
                }
                lineView.isHidden = true
            default: break
            }
        }
    }
    
    var bindType: Int = 0
    
    var accountType: Int? {
        didSet {
            lineView.isHidden = false
            switch accountType {
            case 0:
                titleLabel.text = "服務協議"
            case 1:
                titleLabel.text = "隱私政策"
            case 2:
                titleLabel.text = "刪除帳戶"
                lineView.isHidden = true
            default: break
            }
        }
    }

    var walletType: Int? {
        didSet {
            lineView.isHidden = false
            switch walletType {
            case 0:
                titleLabel.text = "交易歷史"
            case 1:
                titleLabel.text = "解鎖歷史章節"
            case 2:
                titleLabel.text = "獎金歷史"
//            case 3:
//                titleLabel.text = "禮物歷史"
            case 3:
                titleLabel.text = "儲值"
                lineView.isHidden = true
            default: break
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
